package com.example.demomk.Adapter;

import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.example.demomk.databinding.ItemDetailsBinding;
import com.example.demomk.entity.AvgMeasurementWithMeasurements;
import com.example.demomk.entity.Measurement;

import java.util.ArrayList;
import java.util.List;

public class DetailsAdapter extends RecyclerView.Adapter<DetailsAdapter.ViewHolder>{
    private List<Measurement> measurements = new ArrayList<>();
    public void setMeasurements(List<Measurement> measurements) {
        this.measurements = new ArrayList<>(measurements); // 创建新引用
        notifyDataSetChanged();
    }
    @NonNull
    @Override
    public DetailsAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        ItemDetailsBinding binding = ItemDetailsBinding.inflate(LayoutInflater.from(parent.getContext()), parent, false);
        return new ViewHolder(binding);
    }

    @Override
    public void onBindViewHolder(@NonNull DetailsAdapter.ViewHolder holder, int position) {
        Measurement item = measurements.get(position);
        holder.id.setText(String.valueOf(item.getId()));
        holder.avg_note.setText(item.getNotes());
        holder.start_time.setText(item.getStartTime());
//        holder.zero_there.setText(String.valueOf(item.getValue03()));
        holder.avg_zero.setText(String.valueOf(item.getValue05()));
        holder.avg_one.setText(String.valueOf(item.getValue10()));
        holder.avg_two.setText(String.valueOf(item.getValue20()));
        holder.avg_there.setText(String.valueOf(item.getValue30()));
        holder.avg_five.setText(String.valueOf(item.getValue50()));
        holder.avg_ten.setText(String.valueOf(item.getValue100()));
        holder.avg_flow.setText(String.valueOf(item.getFlowRate()));

    }

    @Override
    public int getItemCount() {
        return measurements.size();
    }
    public static class ViewHolder extends RecyclerView.ViewHolder{
        TextView id , avg_note, start_time , zero_there,avg_zero , avg_one ,  avg_two,avg_there , avg_five,avg_ten,avg_flow;
        public ViewHolder(@NonNull ItemDetailsBinding binding )
        {
            super(binding.getRoot());
            id = binding.avgId;
            avg_note = binding.avgNote;
            start_time = binding.avgStartTime;
//            zero_there = binding.zeroThere;
            avg_zero = binding.avgZero;
            avg_one = binding.avgOne;
            avg_two = binding.avgTwo;
            avg_there = binding.avgThere;
            avg_five = binding.avgFive;
            avg_ten = binding.avgTen;
            avg_flow = binding.avgFlow;
        }
    }
}
